package com.hunlian.makelove.adapter;

import android.app.Activity;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hunlian.makelove.R;
import com.hunlian.makelove.common.AppConfig;
import com.hunlian.makelove.common.DateUtils;
import com.hunlian.makelove.common.StringUtil;
import com.hunlian.makelove.bean.Person;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ListAdapterSendInviting extends ModelAdapter<Person> {

	private Activity mContext;
	private ImageLoader mImageLoader;
	private DisplayImageOptions mOptions_ad;
	private String oppositeUserId;
	private SparseArray<Boolean> mSelectState;
	private int mYear;

	public ListAdapterSendInviting(Activity mContext) {
		super();
		this.mContext = mContext;

		// 设置在loading时的默认图片
		mOptions_ad = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher)
				.cacheInMemory(true).cacheOnDisk(true).build();
		mImageLoader = ImageLoader.getInstance();

		if(StringUtil.isEmpty(AppConfig.netTime)){
			String localTime = DateUtils.getLocalLongDate().substring(0,4);
			mYear = Integer.valueOf(localTime);
		}else{
			String localTime = AppConfig.netTime.substring(0,4);
			mYear = Integer.valueOf(localTime);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		MyViewHolder holder = null;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(R.layout.send_inviting_item, null);
			holder = new MyViewHolder();

			holder.mIvHead = (ImageView) convertView.findViewById(R.id.iv_head);
			holder.mTvId = (TextView) convertView.findViewById(R.id.tv_id);
			holder.mTvNickName = (TextView) convertView
					.findViewById(R.id.tv_nickname);
			holder.mTvAddress = (TextView) convertView
					.findViewById(R.id.tv_address);
			holder.mTvEducation = (TextView) convertView
					.findViewById(R.id.tv_education);
			holder.mTvBirthday = (TextView) convertView
					.findViewById(R.id.tv_birthday);
			holder.mRuqest = (TextView) convertView
					.findViewById(R.id.tv_request);
			holder.mIntro = (TextView) convertView.findViewById(R.id.tv_intro);
			holder.mNotHandle = (TextView) convertView.findViewById(R.id.tv_not_handle);
			holder.mAgree = (TextView) convertView.findViewById(R.id.tv_agree);
			convertView.setTag(holder);
		} else {
			holder = (MyViewHolder) convertView.getTag();
		}

		Person bean = null;

		bean = getModel(position);

		mImageLoader
				.displayImage(bean.getPhoto(), holder.mIvHead, mOptions_ad);
		
		holder.mTvId.setText("ID :"+StringUtil.handleNull(bean.getUserId()));
		holder.mTvNickName.setText("昵称 :"+StringUtil.handleNull(bean.getNickname()));
		holder.mTvAddress.setText(StringUtil.handleNull(bean.getAddress()));
		String sEdu = StringUtil.handleNull(bean.getEducation());
		if(sEdu.length()==0){
			holder.mTvEducation.setText("");
		}else{
			holder.mTvEducation.setText(" | "+sEdu);
		}
		String syear = StringUtil.handleNull(bean.getBirthday());
		if(syear.length()==0){
			holder.mTvBirthday.setText("");
		}else{
			int birYear = Integer.valueOf(syear.substring(0, 4));
			holder.mTvBirthday.setText(" | "+(mYear-birYear)+"岁");
		}
		holder.mRuqest.setText("期望 :" + StringUtil.handleNull(bean.getExpect()));
		holder.mIntro.setText("补充介绍 :" + StringUtil.handleNull(bean.getIntroduce()));

		holder.mAgree.setText(StringUtil.handleNull(bean.getStatus()));

		if(StringUtil.handleNull(bean.getStatus()).contains("处理")){
			holder.mAgree.setVisibility(View.GONE);
			holder.mNotHandle.setVisibility(View.VISIBLE);
		}else{
			holder.mAgree.setVisibility(View.VISIBLE);
			holder.mNotHandle.setVisibility(View.GONE);
		}
		return convertView;
	}

	class MyViewHolder {
		private ImageView mIvHead;
		private TextView mTvId;
		private TextView mTvNickName;
		private TextView mTvAddress;
		private TextView mTvEducation;
		private TextView mTvBirthday;
		private TextView mRuqest;
		private TextView mIntro;
		private TextView mNotHandle;
		private TextView mAgree;
	}


}
